using UnityEngine;

namespace ET
{
    public static class ColorExtend
    {

        public static string Color_Red_1 = "ff5356";
        /// <summary>
        /// 返回富文本中的颜色
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strStart"></param>
        /// <returns></returns>
        public static Color ColorRich(this string str, string strStart = "<color=")
        {
            /* 找出指定的2个字符在 该字符串里面的 位置 */
            int strStartIndex = str.IndexOf(strStart);
            // int strEndIndex = str.IndexOf(strEnd)+1;

            /* index为负数 即表示该字符串中没有该字符 */
            if (strStartIndex < 0)
            {
                return Color.white;
            }


            /* 开始截取 */
            string result = str.Substring(strStartIndex + 8, 6);
            return SetHexColor(result);
        }
        /// <summary>
        /// hex转换到color
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static Color SetHexColor( string hex)
        {
            string str = hex + "FFFFFFFF";
            byte br = byte.Parse(str.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            byte bg = byte.Parse(str.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            byte bb = byte.Parse(str.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            byte cc = byte.Parse(str.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
            float r = br / 255f;
            float g = bg / 255f;
            float b = bb / 255f;
            float a = cc / 255f;
            return new Color(r, g, b, a);
        }
        public static Color GetColor(this string hex)
        {
            string str = hex + "FFFFFFFF";
            byte br = byte.Parse(str.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            byte bg = byte.Parse(str.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            byte bb = byte.Parse(str.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            byte cc = byte.Parse(str.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
            float r = br / 255f;
            float g = bg / 255f;
            float b = bb / 255f;
            float a = cc / 255f;
            return new Color(r, g, b, a);
        }

        public static string GetRichText(this string color,string info)
        {
            return string.Format("<color=#{0}>{1}</color>",color,info);
        }
    }
}
